package com.driver.core.mapper.generator;

import com.driver.core.*;
import com.driver.core.mapper.annotation.IMapper;
import com.driver.core.mapper.annotation.Order;

import java.util.List;

public abstract class AbstractXMLGenerator implements IMapper {
    protected final Configuraction config = DBManager.getConfig();
    protected Table table;
    protected List<Column> columns;
    protected String keyColumn = "id";
    protected String keyProperty = "id";

    protected AbstractXMLGenerator(Table table) {
        this.table = table;
        this.columns = table.getColumns();
        for (Column column : columns) {
            if (column.isPrivateKey()) {
                keyColumn = column.getColumnName();
                keyProperty = column.getPropertyName();
            }
        }
    }


    protected AbstractXMLGenerator() {
    }

    @Order(order = 1)
    public String getXMLHead() {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n";

        xml += "<mapper namespace=\"" + config.getPackagePreffix() + "." + config.getMapperPackageName() + "." + table.getClassName() + "Mapper" + "\">\n\n";
        return xml;
    }

    @Order(order = 2)
    public String getResultMap() {
        StringBuilder sb = new StringBuilder();
        sb.append("\t<resultMap id=\"BaseResultMap\" type=\"").append(config.getPackagePreffix()).append(".").append(config.getEntityPackageName()).append(".").append(table.getClassName()).append("\">\n");
        List<Column> columns = table.getColumns();
        for (Column column : columns) {
            if (column.isPrivateKey()) {
                sb.append("\t\t<id column=\"").append(column.getColumnName()).append("\" property=\"").append(column.getPropertyName()).append("\"/>\n");
                break;
            }
        }

        for (Column column : columns) {
            if (!column.isPrivateKey()) {
                sb.append("\t\t<result column=\"").append(column.getColumnName()).append("\" property=\"").append(column.getPropertyName()).append("\"/>\n");
            }
        }

        sb.append("\t</resultMap>\n");
        return sb.toString();
    }


    @Order(order = 3)
    public String getWhereCondition() {
        StringBuilder sb = new StringBuilder();
        sb.append("\n\t<sql id=\"Where_Condition\">\n");
        sb.append("\t   <where>\n");

        for (Column column : columns) {
            sb.append("\t\t   <if test='null != ").append(column.getPropertyName()).append("'>\n");
            sb.append("\t\t\t  and ").append(column.getColumnName()).append(" = #{").append(column.getPropertyName()).append("}\n");
            sb.append("\t\t   </if>\n");
        }
        sb.append("\t   </where>\n");
        sb.append("\t</sql>\n\n");
        return sb.toString();
    }


    @Order(order = 4)
    public String getUpdateWhereCondition() {
        List<Column> columns = table.getColumns();
        StringBuilder sb = new StringBuilder();
        sb.append("\n\t<sql id=\"Update_Where_Condition\">\n");
        sb.append("\t   <where>\n");

        for (Column column : columns) {
            sb.append("\t\t   <if test='null != ").append(StringUtil.lowerFirstName(table.getClassName())).append("Condition.").append(column.getPropertyName()).append("'>\n");
            sb.append("\t\t\t  and ").append(column.getColumnName()).append(" = #{").append(StringUtil.lowerFirstName(table.getClassName())).append("Condition.").append(column.getPropertyName()).append("}\n");
            sb.append("\t\t   </if>\n");
        }

        sb.append("\t   </where>\n");
        sb.append("\t</sql>\n\n");
        return sb.toString();
    }




    @Order(order = 5)
    public String getWhereColumnNameCondition() {
        StringBuilder sb = new StringBuilder();
        sb.append("\n\t<sql id=\"Where_Column_Name_Condition\">\n");
        sb.append("\t   <where>\n");

        for (Column column : columns) {
            sb.append("\t\t   <if test='null != ").append(StringUtil.lowerFirstName(table.getClassName())).append("Condition.").append(column.getPropertyName()).append("'>\n");
            sb.append("\t\t\t  and ").append(column.getColumnName()).append(" = #{").append(StringUtil.lowerFirstName(table.getClassName())).append("Condition.").append(column.getPropertyName()).append("}\n");
            sb.append("\t\t   </if>\n");
        }
        sb.append("\t   </where>\n");
        sb.append("\t</sql>\n\n");
        return sb.toString();
    }


    @Order(order = 6)
    public String getColumnList() {
        List<Column> columns = table.getColumns();

        StringBuilder sb = new StringBuilder();
        sb.append("\t<sql id=\"Column_List\">\n");
        sb.append("\t  ");
        for (Column column : columns) {
            if (DBManager.getConfig().getUrl().contains("sqlserver")) {
                sb.append(column.getColumnName()).append(",");

            }else{
                sb.append("`").append(column.getColumnName()).append("`,");

            }
        }
        sb.setCharAt(sb.length() - 1, ' ');
        sb.append("\n\t</sql>\n\n");
        return sb.toString();
    }


    @Order(order = 10000)
    public String getXMLTail() {
        return "</mapper>";
    }

}
